package com.muyan.singleton.v5;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Author: YLTDMenglc
 * @Date: 2019/6/11
 * @Time: 13:28
 * @Description:
 */
public class TestSingleton5 {

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//        Sinleton5 sinleton1 = Sinleton5.getSinleton();
//        Sinleton5 sinleton2 = Sinleton5.getSinleton();
//        System.out.println(sinleton1 == sinleton2);
//        for (int i = 0; i < 100; i++) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    Sinleton5 singletonV5 = Sinleton5.getSinleton();
//                    System.out.println(Thread.currentThread().getName() + "," + singletonV5);
//                }
//            }).start();
//        }

        // 单例基本原则；保证在单个jvm中不重复创建
//        Sinleton5 sinleton = Sinleton5.getSinleton();
//        // 使用反射破解单例
//        Constructor<Sinleton5> declaredConstructor = Sinleton5.class.getDeclaredConstructor();
//        declaredConstructor.setAccessible(true);
//        Sinleton5 sinleton5 = declaredConstructor.newInstance();
//
//        System.out.println(sinleton == sinleton5);

        ReentrantLockSinleton5 reentrantLockSinleton51 = ReentrantLockSinleton5.getInstance();
        ReentrantLockSinleton5 reentrantLockSinleton52 = ReentrantLockSinleton5.getInstance();
        System.out.println(reentrantLockSinleton51 == reentrantLockSinleton52);

        // 单例基本原则；保证在单个jvm中不重复创建
        ReentrantLockSinleton5 reentrantLockSinleton5 = ReentrantLockSinleton5.getInstance();
        // 使用反射破解单例
        Constructor<ReentrantLockSinleton5> reentrantLockSinleton5Constructor = ReentrantLockSinleton5.class.getDeclaredConstructor();
        reentrantLockSinleton5Constructor.setAccessible(true);
        ReentrantLockSinleton5 reentrantLockSinleton5c = reentrantLockSinleton5Constructor.newInstance();

        System.out.println(reentrantLockSinleton5 == reentrantLockSinleton5c);

    }
}
